﻿using Common.Extensions.OrderBy;
using Common.Extensions.Where;
using DomainModel;
using EmitMapper;
using IManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UI.JsonModel;
using WebPlatform.Models;

namespace WebPlatform.Controllers
{
    public class ArticleController : Controller
    {

        public IManagerArticle IMArticle { get; set; }



        /// <summary>
        /// 文章管理列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 文章分类列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Partition()
        {
            return View();
        }


        /// <summary>
        /// 获取根分类
        /// </summary>
        /// <returns></returns>
        private IEnumerable<SelectListItem> Roots()
        {
            var p = PredicateExtension.True<ArticlePartitionInfo>();
            p = p.And(u => u.Enabled == 1 && u.Level == 0);

            var count = IMArticle.LoadCount(p);
            var list = IMArticle.LoadAll(p, 1, count, new OrderByExpression<ArticlePartitionInfo, string>(a => a.PartitionId));
            var mapper = ObjectMapperManager.DefaultInstance.GetMapper<ArticlePartitionInfo, PartitionVM>();

            var partition = mapper.MapEnum(list).ToList();

            return new SelectList(partition, "PartitionId", "Name");
        }


        [HttpPost]
        public JsonResult GridData(int page = 1, int pagesize = 10)
        {
            GridData<PartitionVM> gridData = new GridData<PartitionVM>();

           

            var p = PredicateExtension.True<ArticlePartitionInfo>();
            p = p.And(u => u.Level == 0);
            var count = IMArticle.LoadCount(p);
            var list = IMArticle.LoadAll(p, 1, 10, new OrderByExpression<ArticlePartitionInfo, string>(a => a.PartitionId));

            var lastList = GetChildren(new ArticlePartitionInfo()
                {
                    HasChildren = 1,
                    Level = 0
                });
            var mapper = ObjectMapperManager.DefaultInstance.GetMapper<ArticlePartitionInfo, PartitionVM>();

            var partition = mapper.MapEnum(lastList).ToList();

            gridData.Rows = partition;
            gridData.Total = count;
            return Json(gridData);
        }



        private IEnumerable<ArticlePartitionInfo> GetChildren(ArticlePartitionInfo partition)
        {
            var pList = new List<ArticlePartitionInfo>();
            if (partition == null || partition.HasChildren == 0)
            {
                return null;
            }
            else
            {
                var where = PredicateExtension.True<ArticlePartitionInfo>();

                if (partition.PartitionId == null)
                {
                    where = where.And(w => w.Level == 0);
                }
                else
                {
                    where = where.And(w => w.ParentId == partition.PartitionId && w.Level == partition.Level + 1);
                }
                var count = IMArticle.LoadCount(where);
                var list = IMArticle.LoadAll(where, 1, count, new OrderByExpression<ArticlePartitionInfo, string>(a => a.PartitionId));
                
                if (list != null && list.Any())
                {
                    foreach (var articlePartitionInfo in list)
                    {
                        pList.Add(articlePartitionInfo);
                        var l = GetChildren(articlePartitionInfo);
                        if (l != null)
                        {
                            pList.AddRange(l);
                        }
                    }
                }
            }
            return pList;
        }




        /// <summary>
        /// 文章分类添加页面
        /// </summary>
        /// <returns></returns>
        public ActionResult OpPartition()
        {
            PartitionVM partition = new PartitionVM();
            partition.Roots = Roots();
            return View(partition);

        }



        /// <summary>
        /// 文章分类添加操作
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult OpPartition(PartitionVM partition)
        {

            if (ModelState.IsValid)
            {
                partition.Enabled = partition.BEnabled ? 1 : 0;
                var mapper = ObjectMapperManager.DefaultInstance.GetMapper<PartitionVM, ArticlePartitionInfo>();
                var p = mapper.Map(partition);

                IMArticle.AddPartition(p);
                return Json(true);
            }
            return Json(false);
            //var p = IMArticle.GetPartition(partition.PartitionId);

            //IMArticle.AddPartition()


        }


    }
}
